﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
using UnityEngine.Events;
using UnityEngine.Sprites;
using UnityEngine.Scripting;
using UnityEngine.Assertions;
using UnityEngine.EventSystems;
using UnityEngine.Assertions.Must;
using UnityEngine.Assertions.Comparers;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

namespace CommonWidget
{
    //用于记录一条信息
    public class WidgetItem
    {
        public WidgetType type;
        public string name;
        public Dictionary<string, Color> colorDic;
        public Dictionary<string, Sprite> spriteDic;
        public Dictionary<string, Sprite> catchSpriteDic;//防止误操作
        public Dictionary<string, Color> catchColorDic;//防止误操作

        public WidgetItem() { }

        public WidgetItem(WidgetType type)
        {
            ChangeType(type);
        }
        
        public void ChangeType(WidgetType type)
        {
            this.type = type;
            InitSpriteDic();
            InitColorDic();
        }
        public WidgetItem(string name,Dictionary<string,Color> colorDic, Dictionary<string, Sprite> spriteDic)
        {
            this.name = name;
            this.colorDic = colorDic;
            this.spriteDic = spriteDic;
        }

        public JSONClass ToJson(string rootPath)//json文件的asset路径的文件夹/
        {
            JSONClass json = new JSONClass();
            json[KeyWord.type] = type.ToString();
            json[KeyWord.name] =string.IsNullOrEmpty(name)?type.ToString():name;
            if (spriteDic != null)
            {
                var dic = json[KeyWord.image] = new JSONClass();
                foreach (var item in spriteDic)
                {
                    if(item.Value != null){
                        dic[item.Key] = GetSpriteGUID(item.Value);
                    }
                }
            }

            if(colorDic != null)
            {
                var dic = json[KeyWord.color] = new JSONClass();
                foreach (var item in colorDic)
                {
                    dic[item.Key] = ColorUtility.ToHtmlStringRGB(item.Value);
                }
            }

            return json;
        }

        private JSONNode GetSpriteGUID( Sprite sprite)
        {
            var texture = sprite.texture;
            var path = AssetDatabase.GetAssetPath(texture);
            
            var textureImporter = TextureImporter.GetAtPath(path) as TextureImporter;
            var guid = AssetDatabase.AssetPathToGUID(path);

            if (textureImporter.spriteImportMode == SpriteImportMode.Multiple)
            {
                var jsonArray = new JSONArray();
                jsonArray.Add(guid);
                jsonArray.Add(sprite.name);
                return jsonArray;
            }
            else
            {
                return guid;
            }
        }

        void InitSpriteDic()
        {
            var keys = WidgetUtility.GetSpriteKeys(type);
            var tempDic = new Dictionary<string, Sprite>();

            if (spriteDic == null)
            {
                spriteDic = new Dictionary<string, Sprite>();
            }
            else
            {
                foreach (var item in spriteDic){
                    Catch(item.Key, item.Value);
                }
                spriteDic.Clear();
            }

            foreach (var item in keys)
            {
                if (!spriteDic.ContainsKey(item))
                {
                    spriteDic.Add(item, TryLoadSprite(item));
                }
            }
        }

        void InitColorDic()
        {
            var keys = WidgetUtility.GetColorKeys(type);

            if (colorDic == null)
            {
                colorDic = new Dictionary<string, Color>();
            }
            else
            {
                foreach (var item in colorDic)
                {
                    Catch(item.Key, item.Value);
                }
                colorDic.Clear();
            }

            foreach (var item in keys)
            {
                if (!colorDic.ContainsKey(item))
                {
                    colorDic.Add(item, TryLoadColor(item));
                }
            }
        }

        void Catch(string key,Sprite sprite)
        {
            if (sprite == null) return;
            if(catchSpriteDic == null){
                catchSpriteDic = new Dictionary<string, Sprite>();
            }
            catchSpriteDic[key] = sprite;
        }

        void Catch(string key,Color color)
        {
            if(catchColorDic == null){
                catchColorDic = new Dictionary<string, Color>();
            }
            catchColorDic[key] = color;
        }

        Sprite TryLoadSprite(string key)
        {
            if(catchSpriteDic != null && catchSpriteDic.ContainsKey(key)){
                return catchSpriteDic[key];
            }
            return null;
        }

        Color TryLoadColor(string key)
        {
            if (catchColorDic != null && catchColorDic.ContainsKey(key))
            {
                return catchColorDic[key];
            }
            return Color.white;
        }
    }
}